package com.qf.day11;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
/*
实现Collection接口实现数据增删改查
 */
public class My2Collection<E> implements Collection<E> {
    private Object[] arr = new Object[10]; //存数据的数组
    private Integer count = 0; //数组中集合个数
    @Override
    public int size() {
        return count;
    }

    @Override
    public boolean isEmpty() {
        return count == 0;
    }

    @Override
    public boolean contains(Object o) {
        for (int i = 0; i < count; i++) {
            if(arr[i].equals(o)){
                return true;
            }
        }
        return false;
    }

    @Override
    public Iterator iterator() {
        return new Iterator() {
            private int index=0;
            @Override
            public boolean hasNext() {
                return  count > index;
            }

            @Override
            public Object next() {
                return arr[index++];
            }
        };
    }

    @Override
    public Object[] toArray() {
        return  Arrays.copyOf(arr,count); //防止null返回
    }
    private void  expansionArr(Object[] arr){
        Arrays.copyOf(arr,arr.length*2);
    }

    @Override
    public boolean add(Object o) {
        arr[count++] = o;
        if(count == arr.length){
            expansionArr(arr);
        }
        return true;
    }

    @Override
    public boolean remove(Object o) {
        for (int i = 0; i < count; i++) {
            if(arr[i].equals(o)){
                System.arraycopy(arr,i+1, arr,i,count-(i+1));
                count--;
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean addAll(Collection c) {
        for (Object o : c) {
           add(o);
        }
        return true;
    }

    @Override
    public void clear() {
        count = 0;
    }

    @Override
    public boolean retainAll(Collection c) { //两个集合相同元素
        ArrayList<Object> arrayList = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            if(!c.contains(arr[i])){ //不相同的放到arrayList中
                arrayList.add(arr[i]);
            }
        }
        return removeAll(arrayList);
    }

    @Override
    public boolean removeAll(Collection c) {
        Boolean flag = false; //此处开关的意义是记录是否进行了删除操作
        for (Object o : c) {
            if(remove(o)){
                flag = true;
            }
        }
        return flag;
    }

    @Override
    public boolean containsAll(Collection c) { //判断是否包含另一个集合
        for (Object o : c) {
            if(!contains(o)){ //有一个元素不在集合中就false
                return false;
            }
        }
        return true;
    }

    @Override
    public Object[] toArray(Object[] a) {
        System.arraycopy(arr,0,a,0,count);
        return a;
    }

    @Override
    public String toString() {
        StringBuffer buffer = new StringBuffer();
        buffer.append("[");
        for (int i = 0 ; i<count;i++) {
            buffer.append(arr[i]+" ");
        }
        buffer.append("]");
        return buffer.toString();
    }
}
